package cn.tl.utils;

import cn.tl.org.mapper.EmployeeMapper;
import cn.tl.user.domain.LoginInfo;

import cn.tl.user.mapper.UserMapper;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

@Component
public class LoginContext {

    //返回值有User或者Employee
    public static Object getCurrentUser(HttpServletRequest request){
        String token = request.getHeader("U-TOKEN");
        //获取spring的容器
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        //从容器里面获取数据
        RedisTemplate redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);
        LoginInfo loginInfo = (LoginInfo)redisTemplate.opsForValue().get(token);
        if(loginInfo.getType()==0){//后台用户
            EmployeeMapper employeeMapper = applicationContext.getBean("employeeMapper", EmployeeMapper.class);
            return employeeMapper.findByLoginInfoId(loginInfo.getId());
        }else {
            //前端用户
            UserMapper userMapper = applicationContext.getBean("userMapper",UserMapper.class);
            return userMapper.findByLoginInfoId(loginInfo.getId());
        }


    }
}
